package com.huawei.wireless;

import android.content.Context;
import android.content.Intent;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import com.huawei.mptcp.upload.UploadDownLoadMachine;
import com.huawei.mptcp.utils.ILogUtil;

import android.content.BroadcastReceiver;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;

/**
 * 
 * @version 1.0.0	<br/>  
 *
 */
public class ConnectionChangeReceiver extends BroadcastReceiver {
	private static final String TAG = ConnectionChangeReceiver.class.getSimpleName();
	
	public static boolean isMobileStarted = false ;
	public static boolean isWifiStarted = false ;
	public static boolean isStart = false;
	
	public static boolean isTestSubFlowTcp = false;
	
	
	public ConnectionChangeReceiver() {
		super();
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		ILogUtil.e(TAG, "received system broadcast");

		ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
	
		State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();

		if (State.CONNECTED == state) {
			ILogUtil.e(TAG, "wifi:"+state);
			
			ILogUtil.e(TAG, "begin transfer data use wifi");
			
			try {
//				UploadDownLoadMachine.socket = new Socket(UploadDownLoadMachine.IP, UploadDownLoadMachine.PORT);
//				UploadDownLoadMachine.inputStream = new DataInputStream(UploadDownLoadMachine.socket.getInputStream());
//				UploadDownLoadMachine.outStream = new DataOutputStream(UploadDownLoadMachine.socket.getOutputStream());
				if(!isWifiStarted && isStart && isTestSubFlowTcp){
					new UploadDownLoadMachine().start();
					isWifiStarted = true;
				}
			}catch (Exception e) {
				ILogUtil.exception(TAG, e);
			}
		}
	
		state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
		if (State.CONNECTED == state) {
			Log.e(TAG, "mbile:"+state);
			
			ILogUtil.e(TAG, "begin transfer data use mobile");
			
			try {
//				UploadDownLoadMachine.socket = new Socket(UploadDownLoadMachine.IP, UploadDownLoadMachine.PORT);
//				UploadDownLoadMachine.inputStream = new DataInputStream(UploadDownLoadMachine.socket.getInputStream());
//				UploadDownLoadMachine.outStream = new DataOutputStream(UploadDownLoadMachine.socket.getOutputStream());
				if(!isMobileStarted && isStart && isTestSubFlowTcp){
					new UploadDownLoadMachine().start();
					isMobileStarted = true;
				}
			}catch (Exception e) {
				ILogUtil.exception(TAG, e);
			}
		}

		
	}
}
